/********************************
* name:	系统管理模块
* func:	1.系统初始化
				2.系统自检
********************************/
#include "system_manager.h"


/**
 * @brief 系统初始化函数
 * @param none
 * @return none
 */
void system_all_func_init(void)
{
  //环形缓冲区初始化
  rt_ringbuffer_init(&uart_ringbuffer_t,uart_ringbuffer_pool,sizeof(uart_ringbuffer_pool));
	//dma初始化
  adc_dma_init();
  //FLASH初始化
	spi_flash_init();
  //OLED初始化
	OLED_Init();
	OLED_Display_On();
  //读取FLASH预存采样时间设置
	char spamle_dat[256];
	memset(spamle_dat, 0, SPI_FLASH_PAGE_SIZE);
	spi_flash_buffer_read(spamle_dat, CONFIG_ADDR_2, SPI_FLASH_PAGE_SIZE);
	sscanf(spamle_dat,"sample:%d",&adc_sample_time);
  //系统上电输出设备ID
  user_printf(&huart3, "====system init====\r\n");
	memset(spamle_dat, 0, SPI_FLASH_PAGE_SIZE);
	spi_flash_buffer_read(spamle_dat, CONFIG_ADDR_4, SPI_FLASH_PAGE_SIZE);
	user_printf(&huart3,"%s\r\n",spamle_dat);
	user_printf(&huart3, "====system ready====\r\n");
  //TF卡存储系统初始化
	tf_test_flag = storage_system_init();
  //初始化日志写入
	log_operation("system init"," ");
  //任务列表初始化
  scheduler_init();
}
static selftest_result_t test_result = {0};//系统自检结果
/**
 * @brief 系统自检函数
 * @param none
 * @return none
 */
selftest_result_t system_self_test(void)
{
    user_printf(&huart3, "====system selftest====\r\n");
    
    // 初始化测试结果
    memset(&test_result, 0, sizeof(selftest_result_t));
    
    // 1. Flash测试
    test_flash_module();
    
    // 2. TF卡测试
    test_tf_card_module();
    
    // 3. RTC测试
    test_rtc_module();
    
    // 4. 计算整体结果
    test_result.overall_pass = test_result.flash_ok && 
                                   test_result.tf_card_ok && 
                                   test_result.rtc_ok;
    if(test_result.overall_pass == 1)
			log_operation("test ok"," ");
    user_printf(&huart3, "====system selftest====\r\n");
    return test_result;
}





//FLASH测试
static void test_flash_module(void)
{
    // 读取Flash ID
		/*
		验证:
		通过读取ID可以确认：
		1.Flash芯片是否存在
		2.SPI通信是否正常
		3.芯片型号是否正确
		*/
    test_result.flash_id = spi_flash_read_id();
    //检查特殊值排除常见故障
    if(test_result.flash_id != 0x00000000 && test_result.flash_id != 0xFFFFFFFF)
    {
				//读取成功,FLASH工作正常
        test_result.flash_ok = true;
				//发送自检信息
        user_printf(&huart1, "flash.........ok\r\n");
    }
    else
    {
				//读取失败
        test_result.flash_ok = false;
				//发送自检信息
        user_printf(&huart3, "flash.........error\r\n");
    }
}
/**
 * @brief TF卡模块测试
 *///user_printf(&huart1, "flash ID: 0x%06lX\r\n", test_result.flash_id);
static void test_tf_card_module(void)
{
    //FRESULT res = f_mount(&SDFatFS, SDPath, 1);
    
		if(tf_test_flag == 0)
		{   test_result.tf_card_ok = true;
        user_printf(&huart3, "TF card.........ok\r\n");
        user_printf(&huart3, "flash ID: 0x%06lX\r\n", test_result.flash_id);
        //使用物理接口获取真实容量
        HAL_SD_CardInfoTypeDef card_info;
        if(HAL_SD_GetCardInfo(&hsd, &card_info) == HAL_OK)
        {
            uint64_t physical_bytes = (uint64_t)card_info.LogBlockNbr * card_info.LogBlockSize;
            test_result.tf_memory_kb = physical_bytes / 1024;
            
            user_printf(&huart3, "TF card memory: %lu KB\r\n", test_result.tf_memory_kb);
        }
		}
    else if(tf_test_flag == -1)
    {
        test_result.tf_card_ok = false;
        user_printf(&huart3, "TF card.........error\r\n");
				user_printf(&huart3, "flash ID: 0x%06lX\r\n", test_result.flash_id);
				user_printf(&huart3, "can not find TF card\r\n");
				log_operation("test error: tf card not found"," ");
    }
}
/**
 * @brief RTC模块测试
 */
static void test_rtc_module(void)
{
    RTC_TimeTypeDef current_time;
    RTC_DateTypeDef current_date;
    
    // 获取RTC时间
    HAL_StatusTypeDef status1 = HAL_RTC_GetTime(&hrtc, &current_time, RTC_FORMAT_BIN);
    HAL_StatusTypeDef status2 = HAL_RTC_GetDate(&hrtc, &current_date, RTC_FORMAT_BIN);
    
    if(status1 == HAL_OK && status2 == HAL_OK)
    {
        // 格式化时间字符串
        snprintf(test_result.rtc_time, sizeof(test_result.rtc_time),
                "%04d-%02d-%02d %02d:%02d:%02d",
                current_date.Year + 2000,
                current_date.Month,
                current_date.Date,
                current_time.Hours,
                current_time.Minutes,
                current_time.Seconds);
        
        test_result.rtc_ok = true;
        user_printf(&huart3, "RTC: %s\r\n", test_result.rtc_time);
    }
    else
    {
        test_result.rtc_ok = false;
        strcpy(test_result.rtc_time, "RTC Error");
        user_printf(&huart3, "RTC: Error\r\n");
    }
}
